package com.feonix.complat.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.feonix.complat.dao.AccessLogDAO;
import com.feonix.complat.entity.AccessLog;

/**
 * 
 * 记录接入数据业务类
 * 
 * @author Feonixzw
 *
 */
@Service("accessLogService")
public class AccessLogService {

	@Autowired
	private AccessLogDAO accessLogDAO;

	private int keepNumber = 3; // 保留最后几次的日志记录

	/**
	 * 记录日志
	 * 
	 * @param log
	 * @return
	 */
	public boolean modifyAccessLog(AccessLog log) {
		boolean isSuccess = false;
		this.deleteExpiredLogs(log.getUserId());
		int iid = accessLogDAO.insert(log);
		if (iid > 0) {
			isSuccess = true;
		}
		return isSuccess;
	}

	/**
	 * 删除过期日志
	 * 
	 * @param loginName
	 */
	private void deleteExpiredLogs(Integer userid) {
		int count = accessLogDAO.findCount(userid);
		if (count >= keepNumber) {
			accessLogDAO.deleteByMinId(userid);
		}
	}

}
